/**   
 *    
 * @file
 * @brief
 * @文件名	: FrameImageView
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-6-6 下午2:16:51 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 加外框的imageView <br>
 * Created 2014-6-6 下午2:16:51
 * 
 * @version
 * @author chenqian
 * 
 * @see
 */

public class FrameImageView extends ImageView {
	private int mFrameColor = Color.WHITE;// 边框颜色
	private int mFrameWidth = 0;// 边框宽度
	private float mFrameAlphaRatio = 1.0f;// 透明度比值，为1时，和内容的透明度一致
	private Paint mFramePaint;// 边框绘制的样式

	/**
	 * Creates a new instance of FrameImageView. <br>
	 * Created 2014-6-6 下午2:36:13
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            int
	 */
	public FrameImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/**
	 * Creates a new instance of FrameImageView. <br>
	 * Created 2014-6-6 下午2:36:24
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 */
	public FrameImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Creates a new instance of FrameImageView. <br>
	 * Created 2014-6-6 下午2:36:29
	 * 
	 * @param context
	 *            Context
	 */
	public FrameImageView(Context context) {
		super(context);
	}

	/**
	 * 初始化边框的样式
	 * 
	 * <br>
	 * Created 2014-6-6 下午2:37:01
	 * 
	 * @author chenqian
	 */
	private void initPaint() {
		mFramePaint = new Paint();
		mFramePaint.setStyle(Paint.Style.STROKE);
		mFramePaint.setColor(mFrameColor);
		mFramePaint.setStrokeWidth(mFrameWidth);
	}

	/**
	 * 设置边框的颜色
	 * 
	 * <br>
	 * Created 2014-6-6 下午2:37:22
	 * 
	 * @param color
	 *            颜色
	 * @author chenqian
	 */
	public void setFrameColor(int color) {
		mFrameColor = color;
		if (null == mFramePaint) {
			initPaint();
		} else {
			mFramePaint.setColor(mFrameColor);
		}
	}

	/**
	 * 设置边框的宽度
	 * 
	 * <br>
	 * Created 2014-6-6 下午2:37:47
	 * 
	 * @param width
	 *            宽度
	 * @author chenqian
	 */
	public void setFrameWidth(int width) {
		mFrameWidth = width;
		if (null == mFramePaint) {
			initPaint();
		} else {
			mFramePaint.setStrokeWidth(mFrameWidth);
		}
	}

	/**
	 * 设置透明度的比值，范围为[0,1]
	 * 
	 * <br>
	 * Created 2014-6-6 下午4:10:58
	 * 
	 * @param ratio
	 *            比值
	 * @author chenqian
	 */
	public void setFrameAlphaRatio(float ratio) {
		if (ratio > 1 || ratio < 0) {
			return;
		}
		mFrameAlphaRatio = ratio;
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// 获取控件需要重新绘制的区域
		Rect rect = canvas.getClipBounds();
		// 保存当前画布
		canvas.save();
		// 裁切画布
		clickFrameRect(canvas, rect);
		// 父类操作会绘制图像
		super.onDraw(canvas);
		// 恢复原画布的绘制区域
		canvas.restore();
		// 绘制边线
		drawFrame(canvas, rect);
	}

	@Override
	public void setAlpha(int alpha) {
		super.setAlpha(alpha);
		if (null == mFramePaint) {
			initPaint();
		}
		mFramePaint.setAlpha((int) (alpha * mFrameAlphaRatio));
		postInvalidate();
	}

	/**
	 * 裁剪Canvas图层 <br>
	 * Created 2014-6-9 上午11:29:49
	 * 
	 * @param canvas
	 *            画布
	 * @param rect
	 *            画布对应的矩形区域
	 * @author chenqian
	 */
	private void clickFrameRect(Canvas canvas, Rect rect) {
		if (null == canvas || null == rect) {
			return;
		}
		int frameWidth = (mFrameWidth >> 1);
		/**
		 * 裁剪Canvas图层，绘制的东西， 只能在裁剪区域的范围能才能显示出来。
		 */
		Rect showRect = new Rect(rect.left + frameWidth, rect.top + frameWidth,
				rect.right - frameWidth, rect.bottom - frameWidth);
		canvas.clipRect(showRect);
	}

	/**
	 * 绘制边线
	 * 
	 * <br>
	 * Created 2014-6-9 上午11:30:32
	 * 
	 * @param canvas
	 *            画布
	 * @param rect
	 *            画布对应的矩形区域
	 * @author chenqian
	 */
	private void drawFrame(Canvas canvas, Rect rect) {
		if (null == canvas || null == rect) {
			return;
		}
		if (null == mFramePaint) {
			initPaint();
		}
		canvas.drawRect(rect, mFramePaint);
	}

}
